$(function () {
    $("#jqGrid").jqGrid({
        url: '../demandmanagement/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
            { label: '结算点', name: 'settlementName', index: 'settlementName'},
            { label: '申请部门', name: 'applyDept', index: 'apply_dept'},
			{ label: '需求岗位', name: 'demandPost', index: 'Demand_post',formatter:getDictInfo},
			{ label: '需求岗位id', name: 'demandPostId', index: 'Demand_post_id',hidden:true},
			{ label: '需求人数', name: 'demandNumber', index: 'demand_Number'},
			{ label: '申请理由', name: 'applicationReasons', index: 'application_Reasons',formatter:getDictInfo },
			{ label: '性别', name: 'sex', index: 'sex' ,
                formatter:function(v){
                    if(v=="0"){
                        return "男";
                    } else if(v=="1") {
                        return "女"
                    }else{
                        return "";
                    }
                } },
			{ label: '最小年龄', name: 'minAge', index: 'min_age' },
            { label: '最小年龄', name: 'maxAge', index: 'max_age' },

            { label: '岗位职责', name: 'jobResponsibilities', index: 'job_responsibilities'},
			{ label: '专业技能需求', name: 'professionalSkills', index: 'Professional_skills' },
            { label: '到位日期', name: 'arrivalDate', index: 'arrival_date' },
            { label: '其他原因', name: 'otherRequirements', index: 'other_requirements' },
            { label: '建议薪酬', name: 'proposedRemuneration', index: 'proposed_remuneration' },
			// { label: '状态', name: 'status', index: 'status' },
			{ label: '结果', name: 'result', index: 'result' },

            { label: '一级审核人', name: 'firApprover', index: 'fir_approver'},
            { label: '一级审核日期', name: 'firApprovalDate', index: 'fir_approval_date'},
            { label: '一级审核说明', name: 'firApprovalNote', index: 'fir_approval_note' },
            { label: '一级审核结果', name: 'firApprovalResult', index: 'fir_approval_result' },

            { label: '二级审核人', name: 'secApprover', index: 'sec_approver' },
            { label: '二级审核日期', name: 'secApprovalDate', index: 'sec_approval_date' },
            { label: '二级审核说明', name: 'secApprovalNote', index: 'sec_approval_note' },
            { label: '二级审核结果', name: 'secApprovalResult', index: 'sec_approval_result' },

            { label: '三级审核人', name: 'thiApprover', index: 'thi_approver' },
            { label: '三级审核日期', name: 'thiApprovalDate', index: 'thi_approval_date' },
            { label: '三级审核说明', name: 'thiApprovalNote', index: 'thi_approval_note' },
            { label: '三级审核结果', name: 'thiApprovalResult', index: 'thi_approval_result' },

            { label: '四级审核人', name: 'fouApprover', index: 'fou_approver' },
            { label: '四级审核日期', name: 'fouApprovalDate', index: 'fou_approval_date' },
            { label: '四级审核说明', name: 'fouApprovalNote', index: 'fou_approval_note' },
            { label: '四级审核结果', name: 'fouApprovalResult', index: 'fou_approval_result' }




        ],
		viewrecords: true,
        height: 360,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        shrinkToFit:false,
        autoScroll: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    // vm.getDictList();
    popPage.popSettlement();

    var r = utils.getDictList("sqly,station,setlever");
    vm.dictList = r.sqly;
    vm.dictList2 = r.station;
    vm.dictList3 = r.setlever;
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
        showtable:false,
        showtable1:false,
		title: null,
		demandManagement: {
            approveType:null


		},
        approveType:null,

        dictList:{},
        dictList2:{},
        dictList3:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
            vm.showtable=false;
            vm.showtable1=false;
			vm.title = "新增";
			vm.demandManagement = {
                sex:0,
                settlementName:""
            };
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.firApprovalResult!=''&&rowData.firApprovalResult!=null)
            {
                alert("数据已经审核过，不能修改");
                return ;
            }
			vm.showList = false;
            vm.showtable=false;
            vm.showtable1=false;

                vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            vm.demandManagement.arrivalDate=$("#arrivalDate").val();

			var url = vm.demandManagement.id == null ? "../demandmanagement/save" : "../demandmanagement/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.demandManagement),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
            var len=ids.length;
            for(var i=0;i<len;i++){
                var rowData=$("#jqGrid").jqGrid("getRowData",ids[i]);
                if(rowData.firApprovalResult!=''&&rowData.firApprovalResult!=null)
                {
                    alert("数据已经审批过，不能删除");
                    return ;
                }
            }
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../demandmanagement/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../demandmanagement/info/"+id, function(r){
                vm.demandManagement = r.demandManagement;
            });
		},
		reload: function (event) {
			vm.showList = true;
            vm.showtable=false;
            vm.showtable1=false;

            var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
		firApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var firApprovalResult=rowData.firApprovalResult;
            if(firApprovalResult!=""&&firApprovalResult!=null){
                alert("一级审核已审批，不能重复审批");
                return;
            }
            vm.showList = false;
            vm.showtable=true;
            vm.showtable1=false;

            vm.title = "审批";

            vm.getInfo(id);
            vm.demandManagement.approveType="fir";
            vm.approveType="fir";
		},
        secApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var firApprovalResult=rowData.firApprovalResult;
            var secApprovalResult=rowData.secApprovalResult;
            if(firApprovalResult=="拒绝"){
                alert("一级审核已拒绝，审批结束");
                return;
            }
            if(firApprovalResult==""||firApprovalResult==null){
                alert("请先进行一级审批");
                return;
            }
            if(secApprovalResult!=""&&secApprovalResult!=null){
                alert("二级审核已审批，不能重复审批");
                return;
            }


            vm.showList = false;
            vm.showtable=true;
            vm.showtable1=false;

            vm.title = "审批";

            vm.getInfo(id);
            vm.approveType="sec";
        },
        thiApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var thiApprovalResult=rowData.thiApprovalResult;
            var secApprovalResult=rowData.secApprovalResult;
            if(secApprovalResult=="拒绝"){
                alert("二级审核已拒绝，审批结束");
                return;
            }
            if(secApprovalResult==""||secApprovalResult==null){
                alert("请先进行二级审批");
                return;
            }
            if(thiApprovalResult!=""&&thiApprovalResult!=null){
                alert("三级审核已审批，不能重复审批");
                return;
            }

            vm.showList = false;
            vm.showtable=true;
            vm.showtable1=false;

            vm.title = "审批";

            vm.getInfo(id);
            vm.approveType="thi";

        },
        fouApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            var thiApprovalResult=rowData.thiApprovalResult;
            var fouApprovalResult=rowData.fouApprovalResult;
            if(thiApprovalResult=="拒绝"){
                alert("三级审核已拒绝，审批结束");
                return;
            }
            if(thiApprovalResult==""||thiApprovalResult==null){
                alert("请先进行三级审批");
                return;
            }
            if(fouApprovalResult!=""&&fouApprovalResult!=null){
                alert("四级审核已审批，不能重复审批");
                return;
            }
            vm.showList = false;
            vm.showtable=true;
            vm.showtable1=false;

            vm.title = "审批";

            vm.getInfo(id);
            vm.approveType="fou";

        },
        approve: function (event) {
            vm.demandManagement.approveType=vm.approveType;
            var url = "../demandmanagement/approve";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.demandManagement),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

        reject: function (event) {
            vm.demandManagement.approveType=vm.approveType;
            var url = "../demandmanagement/reject";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.demandManagement),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
       /* getDictList: function(){
            var params = {
                dicttype:"sqly"
            }
            $.ajax({
                type: "POST",
                url: "../dictutils/getDictList",
                contentType: "application/json",
                data: JSON.stringify(params),
                dataType:"json",
                async:false,
                cache:false,
                success: function(r){
                    if(r.code === 0){
                        vm.dictList = r.sqly;

                    }else{
                        alert(r.msg);
                    }
                }
            });
        },*/

        choose: function(){
            var ids = getSelectedRows();
            if(ids==null){
                return ;
            }
            var len=ids.length;
            for(var i=0;i<len;i++){
                var rowData=$("#jqGrid").jqGrid("getRowData",ids[i]);
                if(rowData.fouApprovalResult==''||rowData.fouApprovalResult==null)
                {
                    alert("数据没有经过审批，不能筛选");
                    return ;
                }
            }
            vm.showTable = false;
            vm.showtable1=true;
            $("#jqGridTable").jqGrid({
                url: '../demandmanagement/choose',
                datatype: "json",
                postData: {"ids":JSON.stringify(ids)},
                colModel: [
                    { label: 'id', name: 'id', index: 'id', key: true, hidden: true },
                    { label: '求职单位', name: 'settlementno', index: 'settlementno' },
                    { label: '用工单位名称', name: 'settlementname', index: 'settlementname' },
                    { label: '需求编号', name: 'damandid', index: 'damandid' },
                    // { label: '岗位编号', name: 'jobid', index: 'jobid' },
                    { label: '岗位名称', name: 'jobname', index: 'jobname' },
                    { label: '姓名', name: 'name', index: 'name' },
                    { label: '性别', name: 'sex', index: 'sex' },
                    { label: '年龄', name: 'age', index: 'topage' },
                    { label: '学历', name: 'education', index: 'education' },
                    { label: '婚否', name: 'married', index: 'married' },
                    { label: '证书名称', name: 'papername', index: 'papername' }
                ],
                viewrecords: true,
                height: 385,
                rowNum: 100,
                //rowList : [10,30,50],
                rownumbers: true,
                //rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPagerTable",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                /*prmNames : {
                 page:"page",
                 rows:"limit",
                 order: "order"
                 },*/
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGridTable").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });

            var page = $("#jqGridTable").jqGrid('getGridParam','page');
            $("#jqGridTable").jqGrid('setGridParam',{
                page:page,
                postData: {"id":JSON.stringify(ids)}
            }).trigger("reloadGrid");

        },
        selected: function(){
            var rowDatas = vm.getRows();
            if(!rowDatas){
                return ;
            }
            $.ajax({
                type: "POST",
                url: "../demandmanagement/selected",
                contentType: "application/json",
                data: JSON.stringify(rowDatas),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });

        },

        getRows: function(){
            //选择多条记录
            var grid = $("#jqGridTable");
            var rowKey = grid.getGridParam("selrow");
            if(!rowKey){
                alert("请选择筛选结果记录");
                return ;
            }

            var ids = grid.getGridParam("selarrrow");
            var rowDatas = new Array();
            ids.forEach(function(val,index){
                rowDatas[index] = $("#jqGridTable").jqGrid("getRowData",val);
            });
            return rowDatas;

        },
        popSettlement: function(){
            var page = $("#popSettlementGrid").jqGrid('getGridParam','page');
            $("#popSettlementGrid").jqGrid('setGridParam',{
                postData:{'isHuashun': 'Y'},
                page:page
            }).trigger("reloadGrid");
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择求职单位",
                area: ['750px', '400px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popSettlement"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var grid = $("#popSettlementGrid");
                    var rowKey = grid.getGridParam("selrow");
                    if(!rowKey){
                        alert("请选择一条记录");
                        return ;
                    }
                    var rowData=grid.jqGrid("getRowData",rowKey);
                    vm.demandManagement.settlementName=rowData.settlementName;
                    vm.demandManagement.settlement=rowData.id;
                    // vm.actAssigndtlList[dtlindex].username=rowData.id;

                    layer.close(index);
                }
            });
        },



    }
});


function arrivalDateChange(value){
    vm.demandManagement.arrivalDate=value;
}